// mircoApp/map_labeling/index/index.js
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");

import {
  debounce
} from "../../../utils/util";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [skinBehavior],
  data: {
    grouping_name: "",
    select: false,
    show: false,
    expire_time: "",
    member_id: '',
    select_id: "",
    statistics: {},
    list: [],
    num: '',
    member: {},
    nickname: "",
    jump_type: "",
    jump_url: "",
    placeholder: "请输入数量",
    is_transfer: 0,
    unused: 0,
    timer: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.get_data()
    this.get_profits()
  },
  bindGrouping() {
    this.setData({
      select: !this.data.select
    });
  },
  selectGrouping(e) {
    console.log(e);
    var grouping_name = e.currentTarget.dataset.datalist.get_type_name;
    let {
      expire_time,
      unused,
      id
    } = e.currentTarget.dataset.datalist
    this.setData({
      select_id: id,
      grouping_name,
      expire_time,
      unused,
      placeholder: "最大数量 " + unused,
      select: false,
    });
  },
  input_handle(e) {
    this.setData({
      num: e.detail.value
    });
  },
  submit() {
    let json = {
      member_id: this.data.member_id,
      num: this.data.num,
      id: this.data.select_id,
    }
    app._postNetWork({
      url: app.getNetAddresss("plugin.map-marker.frontend.controllers.member.index.transfer"),
      data: json,
      success: resdata => {
        let {
          result,
          msg
        } = resdata.data;
        app.tips(msg);
        if (result) {
          this.setData({
            show: false,
            nickname:"",
            grouping_name:"",
            placeholder:"请输入数量",
            member_id:"",
            select_id:"",
            num:''
          });
        }
      }
    })

  },
  record_handle(){
    wx.navigateTo({
      url: '/mircoApp/map_labeling/record/record',
    })
  },
  get_profits() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.map-marker.frontend.controllers.member.index.get-profits"),
      success: resdata => {
        let {
          result,
          data,
          msg
        } = resdata.data;
        if (!result) {
          return app.tips(msg);
        }
        this.setData({
          list: data
        })
      }
    })
  },
  member_handle(e) {
    this.setData({
      member_id: e.detail.value
    });
    if (this.data.timer) {
      clearTimeout(this.data.timer)
      this.data.timer = null;
    }
    this.data.timer = setTimeout(() => {
      debounce(this.get_member())
    }, 600)
  },
  get_member() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.map-marker.frontend.controllers.member.index.search-member"),
      data: {
        member_id: this.data.member_id
      },
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        this.setData({
          nickname: data.nickname||''
        });
      }
    })
  },
  get_data() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.map-marker.frontend.controllers.member.index"),
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        console.log(data, 'data====');
        this.setData({
          member: data.member,
          statistics: data.statistics,
          jump_type: data.jump_type,
          is_transfer: data.is_transfer
        })
        this.data.jump_url = data.jump_url;
      }
    })
  },
  toUrl(e) {
    let { status=0 } = e.currentTarget.dataset;
    let url = '/mircoApp/shop_base/reward_template/index'
    url += `?api=plugin.map-marker.frontend.controllers.marker.index&tabS=${status}`
    wx.navigateTo({
      url: url,
    })
  },
  go(e) {
    let url = e.currentTarget.dataset.url;
    if (url == 'search') {
      wx.navigateTo({
        url: '/packageB/member/category/search_v2/search_v2?tag=map-marker',
      })
    } else if (url == 'store') {
      if (this.data.statistics.unused_total >= 1) {
        wx.navigateTo({
          url: '/mircoApp/map_labeling/store_location/store_location',
        })
      } else {
        app.tips("标注次数不足！")
      }
    }
  },
  onClose(){
    this.setData({show:false});
  },
  popup() {
    this.setData({
      show: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})